package com.los.rabbitmq.confirm;

import com.los.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @program: rabbitmq
 * @description: confirm模式--普通模式
 * 注意：如果已经设置为事务模式，则不允许变成confirm模式
 * @author: Yujuan
 * @create: 2018-08-02 16:55
 **/
public class ConfirmSend2 {
    private static final String QUEUE_NAME="los_confirm_queue_2";
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        //获取一个连接
        Connection connection= ConnectionUtils.getConnection();
        //创建一个通道
        Channel channel=connection.createChannel();
        //通过通道创建一个队列
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        //生产者调用confirmSelect，将channel设置为confirm模式
        channel.confirmSelect();

        String msg="Hello Confirm";
        for (int i=0;i<10;i++
             ) {
            //发送消息
            channel.basicPublish("",QUEUE_NAME,null,msg.getBytes());
        }
        if (!channel.waitForConfirms()){
            System.out.println("message send failed");
        }else {
            System.out.println("message send success");
        }

        channel.close();
        connection.close();
    }
}
